﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unmanned_MSUI.Areas.Admin.Filters;

namespace Unmanned_MSUI.Areas.Admin.Controllers
{
    [Area("Admin")]
    [MyFilter]
    public class MenuInfoController : Controller
    {

        #region 构造函数

        private IMenuInfoBLL _menuInfoBLL;
        public MenuInfoController(
            IMenuInfoBLL menuInfoBLL
            )
        {
            _menuInfoBLL = menuInfoBLL;
        }

        #endregion

        #region 视图
        public IActionResult ListView()
        {
            return View();
        }
        public IActionResult UpdateMenuInfoView()
        {
            return View();
        }
        public IActionResult CreateMenuInfoView()
        {
            return View();
        }
        #endregion

        #region 获取菜单列表的接口（GetMenuInfos）
        [HttpGet]
        public ActionResult GetMenuInfos(int page, int limit, string title)//tilte
        {
            //声明返回对象
            ReturnResult result = new ReturnResult();
            //声明一个总数的变量
            int count;
            var list = _menuInfoBLL.GetMenuInfos(page, limit, title, out count);
            //返回值
            result.Code = 0;
            result.IsSuccess = true;
            result.Msg = "成功";
            result.Count = count;
            result.Data = list;
            //返回结果
            return Json(result);
        }
        #endregion

        #region 返回父级菜单下拉列表数据（GetSelectOptions）
        [HttpGet]
        public ActionResult GetSelectOptions()
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用BLL方法
            var data = _menuInfoBLL.GetSelectOptions();
            //构建返回结果进行返回
            result.Code = 200;
            result.Msg = "获取成功";
            result.IsSuccess = true;
            result.Data = data;
            return Json(result);
        }

        #endregion

        #region  创建菜单的接口（CreateMenuInfo）
        /// <summary>
        /// 创建部门的接口
        /// </summary>
        /// <param name="departmentInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CreateMenuInfo([FromForm] MenuInfo menuInfo)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用添加部门的方法
            string msg;
            result.IsSuccess = _menuInfoBLL.CreateMenuInfo(menuInfo, out msg);
            result.Msg = msg;
            //判断是否添加成功并返回结果
            if (result.IsSuccess)
            {
                result.Code = 200;//修改状态码
            }
            return Json(result);
        }

        #endregion

        #region 根据id获取详情（GetMenuInfoById）
        /// <summary>
        /// 根据id获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetMenuInfoById(string id)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //判断id是否为空
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "id不能为空";
                return Json(result);
            }
            //根据id查询部门信息,并判断是否为空
            MenuInfo menuInfo = _menuInfoBLL.GetMenuInfoById(id);
            if (menuInfo == null)
            {
                result.Msg = "未获取到部门信息";
            }
            else
            {
                //调用获取父级下拉列表和领导下拉列表的接口
                var selectOption = _menuInfoBLL.GetSelectOptions();
                result.Code = 200;
                result.Msg = "查询成功!";

                result.Data = new
                {
                    menuInfo, //返回菜单信息
                    selectOption    //下拉类别信息
                };
            }
            return Json(result);//返回结果
        }

        #endregion

        #region 编辑菜单的接口（UpdateMenuInfo）
        /// <summary>
        /// 修改部门的接口
        /// </summary>
        /// <param name="menuInfo">修改的部门信息</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateMenuInfo([FromForm] MenuInfo menuInfo)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用添加部门的方法
            string msg;
            result.IsSuccess = _menuInfoBLL.UpdateMenuInfo(menuInfo, out msg);
            result.Msg = msg;
            //判断是否添加成功并返回结果
            if (result.IsSuccess)
            {
                result.Code = 200;//修改状态码
            }
            return Json(result);
        }
        #endregion

        #region 菜单软删除的接口（DeleteMenuInfo）
        /// <summary>
        /// 菜单软删除的接口
        /// </summary>
        /// <param name="id">前端传进来的Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteMenuInfo(string id)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "菜单Id不能为空！";
                return Json(result);
            }
            //调用删除方法
            bool isOk = _menuInfoBLL.DeleteMenuInfo(id);
            //判断删除情况
            if (isOk)
            {
                result.Msg = "删除菜单成功！";
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }

        #endregion

        #region 菜单批量软删除的接口（DeleteMenuInfos）
        /// <summary>
        /// 部门批量软删除的接口
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteMenuInfos(List<string> ids)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //非空判断
            if (ids == null || ids.Count == 0)
            {
                result.Msg = "你还没有选择要删除的部门！";
                return Json(result);
            }
            //批量删除的方法
            bool isOk = _menuInfoBLL.DeleteMenuInfo(ids);
            //判断是否成功
            if (isOk)
            {
                result.Msg = "删除成功！";
                result.Code = 200;
            }
            else
            {
                result.Msg = "删除失败！";
            }
            //返回结果
            return Json(result);
        }

        #endregion

        #region 根据登录用户获取能访问的菜单
        [HttpGet]
        public ActionResult GetMenus()
        {
            //返回的菜单信息
            GetMenusDTO res = new GetMenusDTO();
            //构建菜单树
            #region 旧的方法（写死）
            //构建菜单树
            //List<HomeMenuInfoDTO> menuList = new List<HomeMenuInfoDTO>()
            //{
            //    new HomeMenuInfoDTO()
            //    {
            //        Title = "用户管理",
            //        Icon = "fa fa-user",
            //        Href = "/UserInfo/ListView",
            //        Target = "_self"
            //    },
            //    new HomeMenuInfoDTO()
            //    {
            //        Title = "部门管理",
            //        Icon = "fa fa-sitemap",
            //        Href = "/DepartmentInfo/ListView",
            //        Target = "_self"
            //    },
            //    new HomeMenuInfoDTO()
            //    {
            //        Title = "系统管理",
            //        Icon = "fa fa-cog",
            //        Href = "",
            //        Target = "_self",
            //        Child = new List<HomeMenuInfoDTO>()
            //        {
            //            new HomeMenuInfoDTO()
            //            {
            //                Title = "角色管理",
            //                Icon = "fa fa-street-view",
            //                Href = "/RoleInfo/ListView",
            //                Target = "_self"
            //            },
            //            new HomeMenuInfoDTO()
            //            {
            //                Title = "菜单管理",
            //                Icon = "fa fa-navicon",
            //                Href = "/MenuInfo/ListView",
            //                Target = "_self"
            //            }
            //        }
            //    }
            //};
            #endregion

            //获取用户的id
            var staffId = HttpContext.Request.Cookies["StaffId"];
            if (staffId == null)
            {
                res = new GetMenusDTO(new List<HomeMenuInfoDTO>());
                return Json(res);
            }
            else
            {
                List<HomeMenuInfoDTO> menuList = _menuInfoBLL.GetMenus(staffId);
                //给菜单对象赋值子菜单
                //res.MenuInfo[0].Child = menuList;
                res = new GetMenusDTO(menuList);
            }

            return Json(res);
        }



        #endregion
    }
}
